<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

function pick_language()
{
	session_start();
	require_once(APPPATH.'config/language.php');
	
	$url_lang = explode(".", $_SERVER['HTTP_HOST']);
	$CI_URI =& load_class('URI');
	
	$IS_ADMIN_PAGE = $CI_URI->segment(1);
	
	if('admin' == $IS_ADMIN_PAGE){
		// Re-populate $_GET
		parse_str($_SERVER['QUERY_STRING'], $_GET);
		
		// Lang set in URL via ?lang=something
		if ( ! empty($_GET['lang']))
		{
			// Turn en-gb into en
			$lang = substr($_GET['lang'], 0, 2);
		}
		// Lang has already been set and is stored in a session
		elseif ( ! empty($_SESSION['lang_code']) )
		{
			$lang = $_SESSION['lang_code'];
		}
		// Lang has is picked by a user.
		elseif ( ! empty($_COOKIE['lang_code']) )
		{
			$lang = $_COOKIE['lang_code'];
		}
		
		// Still no Lang. Lets try some browser detection then
		elseif ( ! empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
		{
			// explode languages into array
			$accept_langs = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
		
			log_message('debug', 'Checking browser languages: '.implode(', ', $accept_langs));
		
			// Check them all, until we find a match
			foreach ($accept_langs as $lang)
			{
				// Turn en-gb into en
				$lang = substr($lang, 0, 2);
		
				// Check its in the array. If so, break the loop, we have one!
				if(in_array($lang, array_keys($config['supported_languages'])))
				{
					break;
				}
			}
		}
		// If no language has been worked out - or it is not supported - use the default
		if (empty($lang) OR !array_key_exists($lang, $config['supported_languages']))
		{
			$lang = $config['default_language'];
		}
		
		$_SESSION['lang_code'] = $lang;
//     setcookie("lang_code", $lang, time() + 86500, $CI_config->item('cookie_path'),$CI_config->item('cookie_domain'), $CI_config->item('cookie_secure'));
	}else{
		if('en' == $url_lang[0]){
			$lang = 'en';
		}elseif('cn' == $url_lang[0]){
			$lang = 'cn';
		}else{
			$lang = 'en';
// 			$lang = 'tw';
		}
	}
	// Load CI config class
	$CI_config =& load_class('Config');
    // Set the language config. Selects the folder name from its key of 'en'
    $CI_config->set_item('language', $config['supported_languages'][$lang]['folder']);

	// Set php locale time
	if (isset($config['supported_languages'][$lang]['codes']) &&
		sizeof($locale = (array) $config['supported_languages'][$lang]['codes']) > 1)
	{
		array_unshift($locale, LC_TIME);
		call_user_func_array('setlocale', $locale);
		unset($locale);
	}

    // Sets a constant to use throughout ALL of CI.
    define('CURRENT_LANGUAGE', $lang);
}